import {
    QUESTION_TYPE
} from "../services/index.js"

// 单选题选项
class SingleChooseSelection {
    constructor(param) {
        const {
            mark,
            details
        } = param || {}
        this.mark = mark;
        this.details = details;
    }
}

// 连线题选项
class LineLinkSelection {
    constructor(param) {
        const {
            mark,
            keyWord,
            definition
        } = param || {}
        this.mark = mark;
        this.keyWord = keyWord;
        this.definition = definition;
    }
}

// 词汇题
class SubjectWordModel {
    constructor(param) {
        const {
            englishWord,
            chineseTranslation,
            englishTranslation,
            picture,
            audioFrequency
        } = param || {}
        this.englishWord = englishWord;
        this.englishTranslation = englishTranslation;
        this.chineseTranslation = chineseTranslation;
        this.picture = picture;
        this.audioFrequency = audioFrequency;
    }
}

// 单选题
class SubjectSingleChooseModel {
    constructor(param) {
        const {
            subject,
            selection,
            answer
        } = param || {}
        this.subject = subject;
        this.selection = selection.map(item => {
            return new SingleChooseSelection(item);
        });
        this.answer = answer;
    }
}

// 完形填空题
class SubjectClozeModel {
    constructor(param) {
        const {
            subject,
            answer
        } = param || {}
        this.subject = subject;
        this.answer = answer;
    }
}

// 连线题
class SubjectLineLinkModel {
    constructor(param) {
        const {
            subject,
            selection
        } = param || {}
        this.subject = subject;
        this.selection = selection.map(item => {
            return new LineLinkSelection(item);
        })
    }
}

// 题型解析
function parseSubjectContent(subjectContent, questionTypeId) {
    let content = JSON.parse(subjectContent);
    switch (questionTypeId) {
        case QUESTION_TYPE.WORD.value:
            content = new SubjectWordModel(content);
            break;
        case QUESTION_TYPE.SINGLE_CHOOSE.value:
            content = new SubjectSingleChooseModel(content)
            break;
        case QUESTION_TYPE.CLOZE.value:
            content = new SubjectClozeModel(content);
            break;
        case QUESTION_TYPE.LINE_LINK.value:
            content = new SubjectLineLinkModel(content);
            break;
        case QUESTION_TYPE.ALOUD.value:
            // content = new SubjectAloudModel(content)
            break;
    }
    return content;
}

// 题型
export default class QuestionModel {
    constructor(param) {
        const {
            id,
            categoryId,
            articleId,
            questionTypeId,
            questionTypeName,
            questionNumber,
            stem,
            subjectContent
        } = param || {}
        this.id = id;
        this.categoryId = categoryId;
        this.articleId = articleId;
        this.questionTypeId = questionTypeId;
        this.questionTypeName = questionTypeName;
        this.questionNumber = questionNumber;
        this.subjectContent = subjectContent;

        let subjectContentResult = parseSubjectContent(subjectContent, questionTypeId);
        Object.assign(this, subjectContentResult)
    }
}